/*
 * Copyright (C) 2007-2010 Crise, crise<at>mail.berlios.de
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#if !defined(MP_PLUGIN_DLG_H)
#define MP_PLUGIN_DLG_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "resource.h"
#include <MediaPlayersLib/MediaPlayersLib.h>

class MPPluginDlg : public CDialogImpl<MPPluginDlg>
{
public:
	enum { IDD = IDD_MP_PLUGINDLG };

	MPPluginDlg(MPPlugin* p) : plugin(p) { }
	~MPPluginDlg() { }

	BEGIN_MSG_MAP(MPPluginDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		COMMAND_ID_HANDLER(IDOK, OnCloseCmd)
		COMMAND_ID_HANDLER(IDCANCEL, OnCloseCmd)
		COMMAND_RANGE_HANDLER(IDC_WMP_HELP, IDC_ITUNES_HELP, onMediaHelp)
	END_MSG_MAP()

	LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
		// fill values
		SetDlgItemText(IDC_WMP_FORMAT, plugin->toWide(plugin->getOption("WMPSpam")).c_str());
		SetDlgItemText(IDC_ITUNES_FORMAT, plugin->toWide(plugin->getOption("iTunesSpam")).c_str());
		SetDlgItemText(IDC_MPC_FORMAT, plugin->toWide(plugin->getOption("MPCSpam")).c_str());
		CheckDlgButton(IDC_SHOW_MEDIABAR, plugin->getBoolOption("ShowBar") ? BST_CHECKED : BST_UNCHECKED);

		playerCombo.Attach(GetDlgItem(IDC_PLAYER));
		playerCombo.AddString(_T("Winamp"));
		playerCombo.AddString(_T("Windows Media Player"));
		playerCombo.AddString(_T("Media Player Classic"));
		playerCombo.AddString(_T("iTunes"));

		string sPlayer = plugin->getOption("BarPlayer");
		if(strcmp(sPlayer.c_str(), "wmp") == 0) playerCombo.SetCurSel(1);
		else if(strcmp(sPlayer.c_str(), "mpc") == 0) playerCombo.SetCurSel(2);
		else if(strcmp(sPlayer.c_str(), "itunes") == 0) playerCombo.SetCurSel(3);
		else playerCombo.SetCurSel(0);

		SetWindowText(_T(PLUGIN_NAME) _T(" Settings"));

		BOOL bState = plugin->isBarAvailable() ? TRUE : FALSE;
		::EnableWindow(GetDlgItem(IDC_SHOW_MEDIABAR), bState);
		::EnableWindow(GetDlgItem(IDC_PLAYER), bState);

		return TRUE;
	}

	LRESULT onMediaHelp(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
		switch(wID) {
			case IDC_WMP_HELP: MessageBox(MediaPlayersLib::wmpHelp, _T("Windows Media Player Help"), MB_OK); break;
			case IDC_ITUNES_HELP: MessageBox(MediaPlayersLib::itunesHelp, _T("iTunes Help"), MB_OK); break;
			case IDC_MPC_HELP: MessageBox(MediaPlayersLib::mpcHelp, _T("Media Player Classic Help"), MB_OK); break;
		}
		return 0;
	}

	LRESULT OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
		if(wID == IDOK) {
			tstring buf;

			// save values
			buf.resize(::GetWindowTextLength(GetDlgItem(IDC_WMP_FORMAT)) + 1);
			buf.resize(GetDlgItemText(IDC_WMP_FORMAT, &buf[0], static_cast<int>(buf.size())));
			plugin->setOption("WMPSpam", buf.empty() ? "/me playing: %[title] at %[bitrate] <Windows Media Player %[version]>" : plugin->fromWide(buf));

			buf.resize(::GetWindowTextLength(GetDlgItem(IDC_ITUNES_FORMAT)) + 1);
			buf.resize(GetDlgItemText(IDC_ITUNES_FORMAT, &buf[0], static_cast<int>(buf.size())));
			plugin->setOption("iTunesSpam", buf.empty() ? "/me playing: %[title] at %[bitrate] <iTunes %[version]>" : plugin->fromWide(buf));

			buf.resize(::GetWindowTextLength(GetDlgItem(IDC_MPC_FORMAT)) + 1);
			buf.resize(GetDlgItemText(IDC_MPC_FORMAT, &buf[0], static_cast<int>(buf.size())));
			plugin->setOption("MPCSpam", buf.empty() ? "/me playing: %[title] <Media Player Classic>" : plugin->fromWide(buf));

			UINT barCheck = IsDlgButtonChecked(IDC_SHOW_MEDIABAR);
			plugin->setOption("ShowBar", barCheck == BST_CHECKED);

			switch(playerCombo.GetCurSel()) {
				case 1: plugin->setOption("BarPlayer", "wmp"); break;
				case 2: plugin->setOption("BarPlayer", "mpc"); break;
				case 3: plugin->setOption("BarPlayer", "itunes"); break;
				default: plugin->setOption("BarPlayer", "winamp");
			}
		}

		playerCombo.Detach();
		EndDialog(wID);
		return 0;
	}

private:
	CComboBox playerCombo;
	MPPluginDlg(const MPPluginDlg&) { }

	MPPlugin* plugin;
};

#endif // !defined(MP_PLUGIN_DLG_H)